package com.henan.provideservice.controller;

import com.henan.provideservice.api.request.TestRequest;
import com.henan.provideservice.api.response.CommonResponse;
import com.henan.provideservice.api.response.TestResponse;
import com.henan.provideservice.domain.vo.ColumnsRecord;
import com.henan.provideservice.domain.vo.TestBody;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/test")
@AllArgsConstructor
@Slf4j
public class TestController {

    // 不带参数
    @GetMapping("/query/no-params")
    public CommonResponse<String> getWithoutParams() {
        return CommonResponse.ofSuccess("test");
    }

    // 路径参数
    @GetMapping("/query/{name}/{age}")
    public CommonResponse<TestBody> getWithPathParams(@PathVariable String name, @PathVariable Integer age) {
        return CommonResponse.ofSuccess(new TestBody(name, age));
    }

    // 请求体参数
    @GetMapping("/query/with-body")
    public CommonResponse<TestResponse> getWithRequestBody(TestRequest request) {
        return CommonResponse.ofSuccess(new TestResponse(request.getRequestName() + "xx", request.getRequestCode() + "aa"));
    }

    @PostMapping("/create")
    public CommonResponse<TestResponse> create(@RequestBody TestRequest request) {
        CommonResponse<TestResponse> response = CommonResponse.ofSuccess(
                new TestResponse(request.getRequestName() + "post请求", request.getRequestCode() + "post"));

        return response;
    }

    // 抛出 NullPointerException
    @GetMapping("/nullPointerException")
    public String throwNullPointerException() {
        log.error("NullPointerException thrown");
        throw new NullPointerException("This is a NullPointerException");
    }

    // 抛出 IllegalArgumentException
    @GetMapping("/illegalArgumentException")
    public String throwIllegalArgumentException() {
        log.error("IllegalArgumentException thrown");
        throw new IllegalArgumentException("This is an IllegalArgumentException");
    }

    // 抛出 RuntimeException
    @GetMapping("/runtimeException")
    public String throwRuntimeException() {
        log.error("RuntimeException thrown");
        throw new RuntimeException("This is a RuntimeException");
    }

    // 抛出自定义异常
    @GetMapping("/customException")
    public String throwCustomException() {
        log.error("CustomException thrown");
        throw new CustomException("This is a custom exception");
    }

    // 自定义异常类
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public static class CustomException extends RuntimeException {
        public CustomException(String message) {
            super(message);
        }
    }

    @GetMapping("/query/colums")
    public CommonResponse<List<ColumnsRecord>> getColumns() {
        ColumnsRecord column1 = new ColumnsRecord("name", "姓名b", true, null);
        ColumnsRecord column2 = new ColumnsRecord("age", "年龄b", true, null);
        ColumnsRecord column3 = new ColumnsRecord("salary", "数量b", true, null);

        ArrayList<ColumnsRecord> columnsRecords = new ArrayList<>(List.of(column1, column2, column3));

        return CommonResponse.ofSuccess(columnsRecords);
    }
}
